package com.shisfish.library.Utils;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.shisfish.library.Impl.AdminImpl;
import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Model.Admin;
import com.shisfish.library.Model.User;
import com.shisfish.library.Utils.Global.GlobalVariable;

public class LoginUtils extends GlobalVariable{
	
	/**
	 * @author shisfish
	 * @date 2015年12月18日 下午7:51:55
	 * @Description 判断管理员是否登录状态
	 * @param idadmin
	 * @return
	 */
	public static boolean isLoginAdmin(String idadmin, HttpServletRequest request) {
		
		boolean flag = false;
		
		Cache cache = CacheUtils.getCache(ADMIN_LOGIN, request);
		
		if (cache != null) {
			
			Element element1 = cache.get(ISLOGIN);
			Element element2 = cache.get("idadmin");
			if (element1 != null && element2 != null) {
			
				// 1 登录 0 未登录
				if (element2.getObjectValue().equals(idadmin) && element1.getObjectValue().equals("1")) {
					
					List<Admin> admins = new AdminImpl().selectByKey(idadmin);
					
					if (admins.size() > 0) {
						
						Admin admin = admins.get(0);
						
						if (admin.getRecord() == 1) {
							flag = true;
						}
						
					}
					
				}
			
			}
		}
		
		return flag;
	}
	
	public static boolean isLoginUser(String employee, HttpServletRequest request) {
		
		boolean flag = false;
		
		Cache cache = CacheUtils.getCache(USER_LOGIN, request);
		
		if (cache != null) {
			
			Element element1 = cache.get(ISLOGIN);
			Element element2 = cache.get("employee");
			if (element1 != null && element2 != null) {
			
				// 1 登录 0 未登录
				if (element2.getObjectValue().equals(employee) && element1.getObjectValue().equals("1")) {
					
					List<User> users = new UserImpl().selectByKey(employee);
					
					if (users.size() > 0) {
						
						User user = users.get(0);
						
						if (user.getRecord() == 1) {
							flag = true;
						}
						
					}
					
				}
			
			}
		}
		
		return flag;
	}
	
}
